package mvc.command.armory
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;

	import flash.display.DisplayObject;

	import iris.managers.IrisPopUpManager;

	import mvc.command.client.IArmoryTipClient;

	import mx.collections.ArrayCollection;
	import mx.controls.ToolTip;
	import mx.core.Application;
	import mx.core.IToolTip;
	import mx.events.FlexEvent;
	import mx.events.ToolTipEvent;
	import mx.managers.ToolTipManager;

	import nz.co.codec.flexorm.EntityManager;

	import orm.fantasy.TblItemPropertyVO;
	import orm.fantasy.TblItemVO;

	import view.design.DesignArmoryTip;
	import view.design.DesignArmoryTips;
	import view.design.DesignCharacter;
	import view.design.DesignSlot;

	public class ShowArmoryTipCmd implements ICommand
	{
		public function execute(event:CairngormEvent):void
		{
			var client:IArmoryTipClient = event.data.client as IArmoryTipClient;
			if(client.tipTblItemVO == null)
				return;

			// Tip
			var tips:Array = [];
			var armoryTips:DesignArmoryTips = new DesignArmoryTips();
			var toolTipEvent:ToolTipEvent = event.data.toolTipEvent as ToolTipEvent;
			toolTipEvent.toolTip = armoryTips;
			// 鼠标物品
			tips.push(getArmoryTip(client.tipTblItemVO));

			// 装备物品
			var dsnCharacter:DesignCharacter = Application.application.dsnMain.dsnCharacters.tabCharacters.selectedChild as DesignCharacter;
			for each(var designSlot:DesignSlot in dsnCharacter.designSlots)
			{
				if(new ArrayCollection(designSlot.availableSlots).contains(client.tipTblItemVO.slot))
				{
					if(designSlot.rCharItemVO != null
						&& designSlot.tblItemVO.item_id != client.tipTblItemVO.item_id)
						tips.push(getArmoryTip(designSlot.tblItemVO));
				}
			}
			// 追加到显示列表
			for(var i:int = 0; i < tips.length; i++)
			{
//				armoryTips.addChild(tips[tips.length - i - 1] as DisplayObject);
				armoryTips.addChild(tips[i] as DisplayObject);
			}
		}

		private function getArmoryTip(tblItemVO:TblItemVO):DesignArmoryTip
		{
			var em:EntityManager = EntityManager.instance;
			// 鼠标物品
			var tblItemPropVO:TblItemPropertyVO = em.fetchCriteriaFirstResult(
				em.createCriteria(TblItemPropertyVO).addEqualsCondition("info_item_id", tblItemVO.item_id)) as TblItemPropertyVO;
			var armoryTip:DesignArmoryTip = new DesignArmoryTip();
			armoryTip.addEventListener(FlexEvent.INITIALIZE, function():void{
					armoryTip.initilaizeArmory(tblItemVO, tblItemPropVO);});
			return armoryTip;
		}
	}
}